package com.example.tree.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.tree.entity.PublishCritic;
import com.example.tree.entity.dto.PublishCriticDto;
import com.example.tree.entity.vo.PublishCriticVO;
import com.example.tree.mapper.PublishCriticMapper;
import com.example.tree.service.PublishCriticService;
import com.example.tree.util.TreeBuild;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PublishCriticServiceImpl implements PublishCriticService {
    @Resource
    private PublishCriticMapper publishCriticMapper;

    @Override
    public PageInfo<PublishCriticVO> queryList(String articleId) {
        long start = System.currentTimeMillis();
        List<PublishCriticVO> list = new ArrayList<>();
        QueryWrapper<PublishCritic> queryWrapper = new QueryWrapper<>();
        //过滤不要的字段
//        queryWrapper.select(PublishCritic.class, cir -> !cir.getProperty().equals("userId") && !cir.getProperty().equals("userName") && !cir.getProperty().equals("userImage"));
        queryWrapper.eq("delete_critic", 0).eq("article_id", articleId);
        List<PublishCritic> criticList = publishCriticMapper.selectList(queryWrapper);
        PageInfo<PublishCritic> pageInfo = new PageInfo<>(criticList);
        pageInfo.getList().forEach(publishCritic -> {
            PublishCriticVO criticVO = new PublishCriticVO();
            BeanUtils.copyProperties(publishCritic, criticVO);
            criticVO.setCriticId(String.valueOf(publishCritic.getCriticId()));
            criticVO.setArticleId(Long.valueOf(publishCritic.getArticleId()));
            list.add(criticVO);
        });
        //构建树结构数据
        TreeBuild treeBuild = new TreeBuild(list);
        long end = System.currentTimeMillis();
        List<PublishCriticVO> voList = treeBuild.buildTree();
        PageInfo<PublishCriticVO> pageInfoVo = new PageInfo<>(voList);
        BeanUtils.copyProperties(pageInfo, pageInfoVo);
        //将数据重新添加
        pageInfoVo.setList(voList);

        System.err.println("耗时毫秒：" + (end - start));
        return pageInfoVo;
    }

    @Override
    public Integer add(PublishCriticDto publishCriticDto) throws Exception {
        //评论id==null表示顶级 则回复评论人是自己
        if ("".equals(publishCriticDto.getCriticId()) && publishCriticDto.getCriticId() == null) {
            publishCriticDto.setUserId(publishCriticDto.getCriticUserId());
            publishCriticDto.setUserName(publishCriticDto.getCriticUserName());
            publishCriticDto.setUserImage(publishCriticDto.getCriticUserAvatar());
        } else {
            //回复评论
            QueryWrapper<PublishCritic> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("critic_id", publishCriticDto.getCriticId());
            PublishCritic critic = publishCriticMapper.selectOne(queryWrapper);
            publishCriticDto.setUserId(critic.getCriticUserId());
            publishCriticDto.setUserName(critic.getCriticUserName());
            publishCriticDto.setUserImage(critic.getCriticUserAvatar());
        }
        //一二级添加
        if ("".equals(publishCriticDto.getParentId()) && publishCriticDto.getParentId() == null) {
            publishCriticDto.setParentId("0");//父级
            publishCriticDto.setParentCode(String.valueOf(new Date().getTime()));//生成父级唯一编号
        } else {
            //根据场景可添加或者删除
            QueryWrapper<PublishCritic> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("article_id", publishCriticDto.getArticleId());
            queryWrapper.eq("delete_critic", 0);
            queryWrapper.eq("parent_id", 0);//区别是父级
            queryWrapper.eq("critic_user_id", publishCriticDto.getCriticUserId());//评论人id
            queryWrapper.eq("parent_code", publishCriticDto.getParentCode());//区别父级唯一编号
            List<PublishCritic> criticList = publishCriticMapper.selectList(queryWrapper);
            if (!ObjectUtils.isEmpty(criticList)) {
                if (criticList.get(0).getCriticUserId().equals(publishCriticDto.getCriticUserId())) {
                    throw new Exception("自己不能评论自己");
                }
            } else {
                //查询为null 则直接是一级
                publishCriticDto.setParentId("0");//父级
                publishCriticDto.setParentCode(String.valueOf(new Date().getTime()));//生成父级唯一编号
            }
        }
        PublishCritic publishCritic = new PublishCritic();
        BeanUtils.copyProperties(publishCriticDto, publishCritic);
        publishCritic.setCriticTime(new Date());
        return publishCriticMapper.insert(publishCritic);
    }

    @Override
    public List<PublishCritic> queryMyComment(String criticUserId) {
        QueryWrapper<PublishCritic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("critic_user_id", criticUserId);
        queryWrapper.eq("delete_critic", 0);
        List<PublishCritic> list = publishCriticMapper.selectList(queryWrapper);
        if (ObjectUtils.isEmpty(list)) return Lists.newArrayList();
        return list;
    }
}
